const Upyun = require('../../utils/upyun-wxapp-sdk')
const upyun = new Upyun({
  bucket: 'feijiu-img5',
  operator: 'feijiu',
  getSignatureUrl: 'https://zsbpt.zaisubao.cn/index.php/Api/Feijiu/getSignature'
  // getSignatureUrl: 'https://v0.api.upyun.com'
})
let date = new Date;
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();  
const hour = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
const minute = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
const second = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
const milliSeconds = date.getMilliseconds();
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },
  toFabu:function (e) {
    var that = this;
    wx.chooseVideo({
      sourceType: ['album','camera'],
      maxDuration: 60,
      camera: 'back',
      success(r) {
        if (r.size > 10000000) {
           app.tishi('您上传的视频已超过10M!请缩小后再上传!');return false;
        }
        // console.log(r);return false;
        var index= r.tempFilePath.lastIndexOf("\.");
        var kuozhan = r.tempFilePath.substring(index+1,r.tempFilePath.length);
        var name = that.Appendzero(day)+''+hour+''+minute+''+second+''+milliSeconds+''+that.threeRand()+'.'+kuozhan;
        var vodSrc = r.tempFilePath;
        wx.showLoading({
          title: "视频正在上传中"       
        })  
        upyun.upload({
          localPath: vodSrc,
          remotePath: '/video/shop/'+year+'/'+that.Appendzero(month)+'/'+that.Appendzero(day)+'/'+name,
          success: function (res) {
            wx.showToast({
              title: '上传成功',
              icon: 'success',
              duration: 1000
            })            
            if (res.statusCode == 200) {
              res.data = JSON.parse(res.data);
              if (res.data.code == 200) {
                setTimeout(function () {
                  wx.navigateTo({
                    url: 'edit?url='+res.data.url+'&duration='+Math.round(r.duration)
                  })
                },1000)
              }else{
                app.tishi('视频上传失败,请重试!')
              }
              
            }else{
              app.tishi('视频上传失败,请重试!')
            }

          },
          fail: function ({errMsg}) {
            console.log('uploadImage fail, errMsg is', errMsg)
          },
         
        })        
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  fabu:function () {
    
  },
  Appendzero:function(obj){
      if(obj<10) return "0" +""+ obj;
      else return obj;
  },
  threeRand:function () {
    var rand = "";
    for(var i = 0; i < 3; i++){
        var r = Math.floor(Math.random() * 10);
        rand += r;
    }
    return rand;
  } 
})